package com.banmao.algorithm.No1To100.No24;

/**
 *  链表工具类
 */
public class LinkedListUtils {

    // 根据数组生成链表
    public static ListNode generateByArray(int[] arr) {

        if(arr.length == 0) {
            return null;
        }

        ListNode listNode = null;
        for (int i = arr.length - 1; i >= 0; i--) {
            listNode = new ListNode(arr[i], listNode);
        }

        return listNode;

    }

    public static void printLinkedList(ListNode head) {
        StringBuffer sb = new StringBuffer("[");

        ListNode cur = head;
        while (cur != null) {
            sb.append(cur.val + ", ");
            cur = cur.next;
        }
        sb.replace(sb.lastIndexOf(","), sb.length() - 1, "]");
        System.out.println(sb);

    }

}
